package com.yjf.ljr.activity;

import java.util.ArrayList;
import java.util.List;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.CommonAdapter;
import com.yjf.ljr.adapter.CommonAdapter.HandleCallBack;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.HttpConnCallback;
import com.yjf.ljr.dataclass.ChargeSubmitDataClass;
import com.yjf.ljr.dataclass.IdentityInfoDataClass;
import com.yjf.ljr.dataclass.RechargDataClass;
import com.yjf.ljr.dataclass.IdentityInfoDataClass.IdentityInfo;
import com.yjf.ljr.dataclass.RechargDataClass.RechargeInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.util.PaymentCipherDialog;
import com.yjf.ljr.util.PicLoad;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RechargeActivity extends IjActivity implements OnClickListener {
	@ID("etChargerMonery")
	private EditText mEtChargerMonery;	
	@ID("quick_charge")
	private Button quickCharge;
	@ID("WAP_charge")
	private Button WAPCharge;
	
	private String rechargeAmount;
	
	private final int REQUEST_CHARGE_CODE = 1;
	
	public final String QUICK_TYPE = "D";
	public final String WAP_TYPE = "W";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recharge);
		initControl();

	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("充值");	
		quickCharge.setOnClickListener(this);
		WAPCharge.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		rechargeAmount = mEtChargerMonery.getText().toString().trim();
		double amount = 0d;
		try{
			amount = Double.parseDouble(rechargeAmount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		switch (view.getId()) {
		case R.id.quick_charge:
			if (TextUtils.isEmpty(rechargeAmount)) {
				showToast("充值金额不能为空！");
				return;
			}
//			if(amount <= 0) {
//				showToast("充值金额需大于0！");
//				return;
//			}
			new ChargeSubmitTask(rechargeAmount,QUICK_TYPE).execute();
			break;
		case R.id.WAP_charge:
			if (TextUtils.isEmpty(rechargeAmount)) {
				showToast("充值金额不能为空！");
				return;
			}
//			if(amount <= 0) {
//				showToast("充值金额需大于0！");
//				return;
//			}
			new ChargeSubmitTask(rechargeAmount,WAP_TYPE).execute();
			break;

		default:
			break;
		}
	}

	class ChargeSubmitTask extends AsyncTask<Void, Void, String> {
		private ChargeSubmitDataClass dc = new ChargeSubmitDataClass();

		private String amount;
		private String type;
		public ChargeSubmitTask(String amount,String type) {
			this.amount = amount;
			this.type = type;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showProgressDialog();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			RequestObject requestObject = new RequestObject();
			requestObject.method = "chargeSubmit";
			requestObject.map.put("amount", amount);
			requestObject.map.put("type", type);
			return requestDataAndGetErrorMsg(requestObject, dc);

		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			dismissProgressDialog();
			super.onPostExecute(result);
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (!(TextUtils.isEmpty(result))) {
				showToast(result);				
			} else {
				if(!TextUtils.isEmpty(dc.url)) {
					Intent intent = new Intent(RechargeActivity.this,WvWithdrawActivity.class);
					intent.putExtra("url", dc.url);
					intent.putExtra("title", "充值");
					intent.putExtra("returnStr", "userManage/topUp/depositResult");
					startActivityForResult(intent, REQUEST_CHARGE_CODE);
				}
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(requestCode == CommonData.REQUEST_CODE_LOGIN) {
			finish();
			return;
		}
		if(resultCode == RESULT_OK) {
			switch(requestCode) {
			case REQUEST_CHARGE_CODE:
				setResult(RESULT_OK);
				finish();
				break;
			default:
				break;		
			}
		}
	}	
}
